package controllers

import (
	"github.com/astaxie/beego"
	"shuxianghui/models"
	"HTshuxianghui/htmodels"
	"strconv"
	"time"
	"strings"
	"github.com/astaxie/beego/validation"
)

type CommentController struct {
	beego.Controller
}

//带了参数导致bootstrap table无法正常使用
func (c *CommentController)HTCommentList_html() {
	str_Type := c.Ctx.Input.URL()[strings.LastIndex(c.Ctx.Input.URL(), "/") + 1:]
	Type, _ := strconv.Atoi(str_Type)
	c.Data["Type"] = Type
	c.TplName = "comment_list.html"
	c.Render()
}

func (c *CommentController)HTCommentList() {
	Type, _ := c.GetInt("Type")
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{ "Id", "UserId", "Username", "TargetId", "CommentTime", "Content", "StarCount"}
	Comment := []models.Comment{}
	htmodels.HTCommentGetList(&Comment, fileds, Type,  offset, limit, search, "TargetId", orderby)
	count, _ := htmodels.HTCommentGetCount(search, "TargetId", Type, )
	c.Data["json"] = map[string]interface{}{"rows":&Comment, "total":count}
	c.ServeJSON()
}


func (c *CommentController)HTCommentDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetCommentDetailInfo(c, Id)
	}
	mytype,_:=c.GetInt("type")
	c.Data["Type"]=mytype
	c.TplName = "comment_detail.html"
}

func SetCommentDetailInfo(c *CommentController, Id int64) {
	Comment := models.Comment{Id:int64(Id)}
	htmodels.HTCommentGetDetail(&Comment)
	c.Data["Comment"] = &Comment
}

func (c *CommentController)HTCommentAdd() {
	Comment := models.Comment{}
	if err := c.ParseForm(&Comment); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("commenttime"));err!=nil{
		Comment.CommentTime=time.Now()
	}else{
		Comment.CommentTime=time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&Comment); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTCommentAdd(&Comment); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/comment/detail`, "添加成功！", "", false)
}


func (c *CommentController)HTCommentUpdate() {
	Id , _ := c.GetInt64("Id");
	Comment := models.Comment{Id:int64(Id)}
	if err:=htmodels.HTCommentGetDetail(&Comment);err!=nil{
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！" + err.Error(), "", true)
	}
	if err := c.ParseForm(&Comment); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("commenttime"));err!=nil{
		Comment.CommentTime=time.Now()
	}else{
		Comment.CommentTime=time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&Comment); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTCommentUpdate(&Comment); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/comment/list_html`+`/`+strconv.Itoa(Comment.Type), "更新成功！", "", false)
}


func (c *CommentController)HTCommentDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTCommentDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "删除" + strconv.FormatInt(count, 10) + "行")
}

